package com.yz.bi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yz.api.facade.bi.BiFacade;
import com.yz.model.vo.bi.ChartVo;
import com.yz.bi.service.BiService;
import com.yz.common.resp.BaseResponse;
import com.yz.common.utils.ResultUtils;
import com.yz.model.dto.chart.ChartAddRequest;
import com.yz.model.dto.chart.ChartQueryRequest;
import com.yz.model.dto.chart.ChartUpdateRequest;
import com.yz.model.dto.chart.GenChartByAiRequest;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Retryable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * <p>
 * User:Mr.Du
 * Date:2024/4/10
 * Time:19:04
 */
@Slf4j
@RestController
public class BiController implements BiFacade {

    @Resource
    private BiService biService;

    @Override
    public BaseResponse<Long> addChart(Long uid, ChartAddRequest chartAddRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(chartAddRequest == null){
            throw new RuntimeException("参数为空");
        }
        Long id = biService.addChart(uid,chartAddRequest);
        if(id == null) {
            throw new RuntimeException("生成图表失败");
        }
        return ResultUtils.success(id);
    }

    @Override
    @Retryable
    public BaseResponse<ChartVo> genChartByAi(Long uid, MultipartFile multipartFile, GenChartByAiRequest genChartByAiRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(multipartFile == null){
            throw new RuntimeException("文件不能为空为空");
        }
        if(genChartByAiRequest == null){
            throw new RuntimeException("参数为空");
        }
        ChartVo chartVo = biService.genChartByAi(uid, multipartFile, genChartByAiRequest);
        if(chartVo == null){
            throw new RuntimeException("生成图表失败");
        }
        return ResultUtils.success(chartVo);
    }

    @Override
    @Retryable
    public BaseResponse<ChartVo> genChartByAiAsync(Long uid, MultipartFile multipartFile, GenChartByAiRequest genChartByAiRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(multipartFile == null){
            throw new RuntimeException("文件不能为空为空");
        }
        if(genChartByAiRequest == null){
            throw new RuntimeException("参数为空");
        }
        ChartVo chartVo = biService.genChartByAiAsync(uid, multipartFile, genChartByAiRequest);
        if(chartVo == null){
            throw new RuntimeException("生成图表失败");
        }
        return ResultUtils.success(chartVo);
    }

    @Override
    public BaseResponse<ChartVo> genChartByAiAsyncMq(Long uid, MultipartFile multipartFile, GenChartByAiRequest genChartByAiRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(multipartFile == null){
            throw new RuntimeException("文件不能为空为空");
        }
        if(genChartByAiRequest == null){
            throw new RuntimeException("参数为空");
        }
        ChartVo chartVo = biService.genChartByAiAsyncMq(uid, multipartFile, genChartByAiRequest);
        if(chartVo == null){
            log.error("genChartByAiAsyncMq error");
            throw new RuntimeException("生成图表失败");
        }
        return ResultUtils.success(chartVo);
    }

    @Override
    public BaseResponse<Boolean> deleteChart(Long uid, Long id) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(id == null){
            throw new RuntimeException("参数为空");
        }
        boolean b = biService.deleteChart(uid, id);
        if(!b){
            throw new RuntimeException("删除失败");
        }
        return ResultUtils.success(true);
    }

    @Override
    public BaseResponse<Boolean> updateChart(Long uid, ChartUpdateRequest chartUpdateRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(chartUpdateRequest == null){
            throw new RuntimeException("参数为空");
        }
        boolean b = biService.updateChart(uid, chartUpdateRequest);
        if(!b){
            throw new RuntimeException("更新失败");
        }
        return ResultUtils.success(true);
    }

    @Override
    public BaseResponse<Page<ChartVo>> listChartByPage(ChartQueryRequest chartQueryRequest) {
        if(chartQueryRequest == null){
            throw new RuntimeException("参数为空");
        }
        Page<ChartVo> biVoPage = biService.listChartByPage(chartQueryRequest);
        if(biVoPage == null){
            throw new RuntimeException("获取失败");
        }
        return ResultUtils.success(biVoPage);
    }

    @Override
    public BaseResponse<Page<ChartVo>> listMyChartByPage(Long uid, ChartQueryRequest chartQueryRequest) {
        if(uid == null){
            throw new RuntimeException("用户未登录");
        }
        if(chartQueryRequest == null){
            throw new RuntimeException("参数为空");
        }
        Page<ChartVo> biVoPage = biService.listMyChartByPage(uid, chartQueryRequest);
        if(biVoPage == null){
            throw new RuntimeException("获取失败");
        }
        return ResultUtils.success(biVoPage);
    }
}
